--[[
	service，群发邮件
]]

local filename = "massmail.lua"
local skynet = require "skynet"
local cluster = require "skynet.cluster"

require "skynet.manager"

local json = require "cjson.safe"
json.encode_sparse_array(true,1,1)

local redisdao = require "redisdao"
local filelog = require "filelog"
local timetool = require "timetool"
local tabletool = require "tabletool"

local routerbase = require "routerbase"

local MassMail = routerbase:new()

--群发邮件列表
local massmail_list = {}

--群发邮件keyid列表
local massmail_keys = {}

--最大邮件数量，20封
local max_massmail_num = 20

--默认有效时间,3天
local default_valid_time = 3 * 86400

-- 每次gm启动，从redis里面获取群发邮件列表
function MassMail.CMD.get_massmail_from_redis()
	local result, result_data = redisdao.query_data(".gmmqredis", "get", "massmail_list")
	if result_data then
		local massmail_data = json.decode(result_data) or {}
        local cur_time = os.time() + 3600   --邮件有效时间至少要大于1小时
        for _,item in ipairs(massmail_data) do  --剔除过期的的邮件
            if item.valid_time > cur_time then
                table.insert(massmail_list,item)
                massmail_keys[item.keyid] = true
            end
        end
	end
    MassMail.CMD.renew_massmail(true)

	return true	
end

--更新群发邮件
function MassMail.CMD.renew_massmail(is_notnotify_gate)
    --更新redis值,并通知gate
	redisdao.query_data(".gmmqredis", "set", "massmail_list", json.encode(massmail_list or {}))
    if not is_notnotify_gate then
	    skynet.pcall(cluster.send, "gatenode", ".router", "setmassmail", tabletool.deepcopy(massmail_list))
    end
	return true
end

--发送群发邮件，通知所有在线玩家
--msgbody = {keyid , fields, valid_time} keyid邮件唯一id, fields邮件内容
--valid_time有效时间，时间戳，超过该时间自动删除,默认3天
function MassMail.CMD.send_massmail(msgbody)
    msgbody = msgbody or {}
    msgbody.keyid = tonumber(msgbody.keyid) or 0
    msgbody.valid_time = tonumber(msgbody.valid_time) or ( os.time() + default_valid_time)

    local keyid = msgbody.keyid or 0
    if keyid == 0 or not msgbody.fields then
        return false
    end

    --群发邮件不带附件
    msgbody.fields.attach_json = nil

    --新邮件加入群发邮件列表
    if massmail_keys[keyid] then
        filelog.sys_error("------mail is already exist!------", msgbody)
        return false
    end
    while ( #massmail_list >= max_massmail_num) do
        local rm_item = table.remove(massmail_list,1)
        massmail_keys[rm_item.keyid] = nil
    end
    massmail_keys[keyid] = true
    table.insert(massmail_list,tabletool.deepcopy(msgbody))

    --刷新并发送群发邮件
    MassMail.CMD.renew_massmail()
    skynet.pcall(cluster.send, "gatenode", ".router", "send_massmail", msgbody)

	return true
end

--删除群发邮件
function MassMail.CMD.del_massmail(keyid)
    if not keyid or not massmail_keys[keyid] then
        return false
    end

    local idx = nil
    for i,item in pairs(massmail_list) do
        if item.keyid == keyid then
            idx = i
            break
        end
    end
    if idx and massmail_list[idx] then
        table.remove(massmail_list,idx)
        massmail_keys[keyid] = nil
        MassMail.CMD.renew_massmail()
    end

    return true
end

--获取群发邮件
function MassMail.CMD.get_massmail()
    return tabletool.deepcopy(massmail_list)
end

skynet.start(function ()
	MassMail:start()
end)
